/*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public
* License Version 1.1 ("License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/MPL-1.1.html
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is: CalemEAM MicroLocale
*
* The Initial Developer of the Original Code is CalemEAM Inc.
* Portions created by CalemEAM are Copyright (C) 2008 CalemEAM Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* ***** END LICENSE BLOCK *****
*/

package com.calemeam.text;

import java.util.Calendar;
import java.util.Date;
import java.util.Vector;

/**
 * Utility class
 * @author cli
 */
public class MicroFormatUtil {
   
   public static Calendar getBlankCalendar() {
      return getBlankCalendar(true);
   }
   public static Calendar getBlankCalendar(boolean resetTime) {
      Calendar cn=Calendar.getInstance();
      cn.set(Calendar.YEAR, 1970);
      cn.set(Calendar.MONTH, Calendar.JANUARY);
      cn.set(Calendar.DAY_OF_MONTH, 1);
      if (resetTime) {
         cn.set(Calendar.HOUR_OF_DAY, 0);
         cn.set(Calendar.MINUTE, 0);
         cn.set(Calendar.SECOND, 0);
      }
      return cn;
   }
   
   public static String getDateSeg(Object ob, int seg) {
      return Integer.toString(getDateSegInt(ob, seg));
   }
   
   public static int getDateSegInt(Object ob, int seg) {
      Date dt=(Date)ob;
      Calendar cn=Calendar.getInstance();
      cn.setTime(dt);
      return cn.get(seg);
   }
   
   public static void callObject(Object ob, int seg, int v) throws MicroParsingException {
      if (ob==null) return;
      if (ob instanceof Calendar) {
         ((Calendar)ob).set(seg, v);
      }
   }
   
   public static Vector resFromArray(Object[] ar) {
      MicroResIf mi=MicroFactory.getRes();
      Vector v=new Vector();
      for (int i=0; i< ar.length; i++) v.addElement(mi.get(ar[i]));
      return v;
   }
   
   public static boolean isDigit(char c) {
      return (c>=0x30 && c<=0x39);
   }
   /**
    * Remove chars from a given array for reg.replace(/[^abc]/g, "");
    */
   public static String includeChars(String src, String include) {
      StringBuffer sb=new StringBuffer();
      for (int i=0; i< src.length(); i++) {
         if (include.indexOf(src.charAt(i))!=-1) sb.append(src.charAt(i));
      }
      return sb.toString();
   }
   public static String excludeChars(String src, String exclude) {
      StringBuffer sb=new StringBuffer();
      for (int i=0; i< src.length(); i++) {
         if (exclude.indexOf(src.charAt(i))==-1) sb.append(src.charAt(i));
      }
      return sb.toString();
   }
   /**
    * Clone a vector
    */
   public static Vector clone(Vector src) {
      Vector v=new Vector();
      for (int i=0; i< src.size(); i++) v.addElement(src.elementAt(i));
      return v;
   }
}